﻿using System;

namespace Dropthings.Widget.Widgets.CnWeather
{
    public class WeatherModel
    {
        public WeatherModel()
        {
        }

        private string _time = string.Empty;
        public string Time
        {
            get
            {
                return this._time;
            }
            set
            {
                this._time = value;
            }
        }
        private string _temperature = string.Empty;
        public string Temperature
        {
            get
            {
                return this._temperature;
            }
            set
            {
                this._temperature = value;
            }
        }
        private string _description = string.Empty;
        public string Description
        {
            get
            {
                return this._description;
            }
            set
            {
                this._description = value;
            }
        }
        private string _wind = string.Empty;
        public string Wind
        {
            get
            {
                return this._wind;
            }
            set
            {
                _wind = value;
            }
        }
        private string _imageSouce = string.Empty;

        public string ImageSource
        {
            get
            {
                return _imageSouce;
            }
            set
            {
                _imageSouce = value;
            }
        }
    }

    public class WeatherData
    {
        private static WeatherData _weather = null;
        private string[] data = null;
        private string _city = string.Empty;
        public string City
        {
            get
            {
                return this._city;
            }
            set
            {
                this._city = value;
            }
        }
        static WeatherData()
        {
            if (_weather == null)
            {
                _weather = new WeatherData();
            }
        }
        public static WeatherData GetInstance()
        {
            return _weather;
        }

        private const string SCITY = "无锡";

        private WeatherData()
        {
            this._city = SCITY;
            GetWeatherData();
        }

        public void GetWeatherData()
        {
            if (this._city == string.Empty)
            {
                this._city = SCITY;
            }
            try
            {
                data = getCityWeather(this._city);
            }
            catch
            {

            }
            if (data == null || data.Length < 1 || string.IsNullOrEmpty(data[1]))
            {
                data = WeatherStateCach.Instance.DeSerializeWeather();
            }
            else
            {
                WeatherStateCach.Instance.SaveWeatherSate(data);
            }
        }
        public string PageName
        {
            get;
            set;
        }
        public string UserControlName
        {
            get;
            set;
        }
        public WeatherModel GetWeatherData(DayTime time)
        {
            WeatherModel model = new WeatherModel();
            if (this._city == string.Empty)
            {
                this._city = SCITY;
            }
            if (data == null || data.Length < 1 || string.IsNullOrEmpty(data[1]))
            {
                data = WeatherStateCach.Instance.DeSerializeWeather();
            }
            if (time == DayTime.Today)
            {
                model.Time = data[4].Split(' ')[0];
                model.Temperature = data[5].Replace("&nbsp;", "");
                model.Description = GetDiscription(data[6]);
                model.ImageSource = GetBitImage(data[8], data[9]);//首次调用要用WeatherState，后边的调用要用weatherState
                model.Wind = data[7];
            }
            else if (time == DayTime.Tomorrow)
            {
                string strTime = data[4];
                DateTime dTime = DateTime.Now;
                DateTime.TryParse(strTime, out dTime);
                model.Time = dTime.AddDays(1.0).ToString("yyyy-M-d");
                model.Temperature = data[12].Replace("&nbsp;", "");
                model.Description = GetDiscription(data[13]);
                model.ImageSource = GetBitImage(data[15], data[16]);
                model.Wind = data[14];
            }
            else if (time == DayTime.AfterDay)
            {
                string strTime = data[4];
                DateTime dTime = DateTime.Now;
                DateTime.TryParse(strTime, out dTime);
                model.Time = dTime.AddDays(2.0).ToString("yyyy-M-d");
                model.Temperature = data[17].Replace("&nbsp;", "");
                model.Description = GetDiscription(data[18]);
                model.Wind = data[19];
                model.ImageSource = GetBitImage(data[20], data[21]);
            }
            return model;
        }

        private string GetDiscription(string discrip)
        {
            string temp = string.Empty;
            if (discrip.Trim().Split(' ').Length > 1)
            {
                temp = discrip.Trim().Split(' ')[1];
            }
            return temp;
        }

        private string GetBitImage(string imagefrom, string imageto)
        {
            try
            {
                string imageName = GetWeatherPathName(imagefrom, imageto);
                string path = "~/Widgets/Cnweather/" + imageName;
                return path;
            }
            catch 
            {
                return string.Empty;
            }
        }

        private string GetWeatherPathName(string from, string to)
        {
            string fromname = GetFileNameOnly(from);
            string toname = GetFileNameOnly(to);
            string combinename = fromname + toname;
            if (combinename == "12" || combinename == "21")
            {
                return "cloudy.png";
            }
            else if (combinename == "72" || combinename == "27")
            {
                return "cloudtorain.png";
            }
            else
            {
                return fromname + ".png";
            }
        }

        private string GetFileNameOnly(string name)
        {
            return name.Split('.')[0];
        }

        public string[] getCityWeather(string cityCode)
        {
            try
            {
                CnWeatherWebService.WeatherWebService weather = new CnWeatherWebService.WeatherWebService();
                weather.Timeout = 5000;
                string[] weatherArray = weather.getWeatherbyCityName(cityCode);
                return weatherArray;
            }
            catch (Exception)
            {

            }
            return null;

        }
    }

    public enum DayTime { Today = 1, Tomorrow, AfterDay }
}
